import { defineStore } from 'pinia'
import { saveLocal, removeLocal, getLocal } from '@/utils'
import { findAccount } from '@/api'
import { inject } from 'vue'

export const useUserInfoStore = defineStore('UserInfoStore', {
    state: () => {
        const socket: any = inject('socket')
        const userInfo = getLocal('USER_INFO')
        if (userInfo) {
            socket.emit('reOpen', userInfo.id)
            return { userInfo }
        } else {
            return {userInfo:{}}
        }


        // return { userInfo: getLocal('USER_INFO') || {} }
    },
    actions: {
        //本地保存用户信息
        async saveUserInfo(uuid: string) {
            try {
                const res = await findAccount({ uuid })
                const { data: { data } } = res
                const userInfo: any = {}
                Object.keys(data).forEach(item => {
                    return data[item] && item !== 'password' && item !== 'updateTime' && (userInfo[item] = data[item])
                })
                this.$state.userInfo = userInfo
                saveLocal('USER_INFO', userInfo)

            } catch (error) {
            }
        },
        updateUserInfo: (res: any) => {

        },

        removeUserInfo(data: any) {
            removeLocal(data)
        }

    }
})
